<?php

namespace App\Repositories;

use App\Models\AccountRecord;
use App\Models\ElectPoolJoin;
use App\Models\Member;
use App\Models\MemberVisit;

class MemberVisitRepository
{
    public function saveOrUpdate($uid = null, $to_uid = null, $type = null, $object_id = null)
    {
        if (!intval($uid) || !intval($to_uid)) return false;
        if ($uid == $to_uid) return false;

        $res = MemberVisit::where(['uid' => $uid, 'df_uid' => $to_uid])->first(['id', 'pv', 'update_time']);
        $res = $res ? $res->toArray() : [];

        $member = Member::where(['id' => $uid])->first();
        if (empty($member) || empty($member['avatar']) || empty($member['weixin']) || empty($member['undergraduate']) || empty($member['birthday']) || empty($member['height'])) {
            return false;
        }

        if ($res) {
            MemberVisit::where("id", "=", $res['id'])->update([
                'pv' => intval($res['pv']) + 1,
                'object_type' => intval($type),
                'object_id' => intval($object_id)
            ]);
        } else {
            MemberVisit::create([
                'uid' => $uid,
                'df_uid' => $to_uid,
                'pv' => 1,
                'object_type' => intval($type),
                'object_id' => intval($object_id)
            ]);
        }

        //一小时内只发一次
        if (strtotime($res['update_time']) > strtotime("-1 hour"))
            return false;

        //判断是否已解锁
        $account_record_id = AccountRecord::where(['uid' => $to_uid, 'df_uid' => $uid, 'type' => 58])->value("id");
        if ($account_record_id) {
            switch ($type) {
                case 1:
                    $join_id = ElectPoolJoin::where(['elect_pool_id' => $object_id, 'uid' => $uid])->orderBy("id", "desc")->value("id");
                    $link = "elect-pool-join/member?eid=" . $object_id . '&ejid=' . $join_id;
                    break;
                case 2:
                    $link = "nearby/member?id=" . $to_uid;
                    break;
                case 3:
                    $link = "share/member?uid=" . md6($to_uid);
                default:
                    break;
            }
        }

        //发送模板消息
        $to_member = Member::where(['id' => $to_uid])->first();
        if (!empty($to_member['openid']) && $to_member['is_send_visit_msg'] == 1) {
            sendTemplateMsg($to_member['openid'], 23, [
                'username' => "你有新的" . ($to_member['gender'] == 'F' ? "小哥哥" : "小姐姐") . "来访",
                'page' => $link ?? "member-visit/index"
            ], 19);
        }

        return true;
    }

    public function getVisitPages($filter = [], int $pageSize = 10, int $pageNum = 1, $order = [])
    {
        $res = Member::from("member as m")
            ->join("member_visit as mv", "m.id", "=", "mv.uid")//访问我的
//            ->join("member_visit as mv", "m.id", "=", "mv.df_uid")//访问我的
            ->join("member_visit_order as mvo", function ($join) {
                $join->on("mvo.df_uid", "=", "mv.uid")->where("mvo.uid", "=", UID);
            }, null, null, 'left')
            ->where($filter)
            ->orderBy("mv.update_time", "desc")
            ->paginate($pageSize, ['m.id', 'm.username', 'm.residence', 'm.gender', 'm.lcity', 'm.avatar', 'mv.update_time as last_active_time', 'm.education', 'm.birthday', 'mv.pv', 'mvo.id as order_id', 'm.cancel', 'm.occupation'], null, $pageNum)
            ->toArray();

        $res = filterPaginateData($res);

        foreach ($res['items'] as $k => &$v) {
            if ($v['last_active_time']) {
                if (time() - strtotime($v['lastest_visit_time']) < 60 * 60 * 1) {
                    $v['txt_1'] = "刚刚活跃";
                }
            }

            $v['lcity'] = $v['lcity'] ?? "";
        }

        return $res;
    }

    /**
     * 删除
     * @param null $uid
     * @param null $to_uid
     * @return bool|mixed|null
     */
    public function delete($uid = null, $to_uid = null)
    {
        if (!$uid || !$to_uid) return false;
        return MemberVisit::where(['uid' => $uid, 'df_uid' => $to_uid])->delete();
    }
}